package sicnu.cs.ich.log.rest;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sicnu.cs.ich.api.common.ResultInfo;
import sicnu.cs.ich.api.domain.entity.LoginLog;
import sicnu.cs.ich.api.domain.entity.OperationLog;
import sicnu.cs.ich.common.util.ResultInfoUtil;
import sicnu.cs.ich.log.rest.form.LogPageParam;
import sicnu.cs.ich.log.rest.form.LoginPageParam;
import sicnu.cs.ich.log.service.ILogService;

import java.io.IOException;

/**
 * @author CaiKe
 * @date 2021/12/5 16:40
 */
@Slf4j
@RestController
@RequestMapping("/logs")
@RequiredArgsConstructor
@Api(tags = {"日志模块"})
public class LogResource {

    private final ILogService logService;

    @GetMapping("/oper")
    @ApiOperation(value = "获取操作日志")
    public ResultInfo<PageInfo<OperationLog>> getLogs(LogPageParam pageParam) {
        return ResultInfoUtil.buildSuccess(logService.getOperationLog(pageParam));
    }

    @GetMapping("/log")
    @ApiOperation(value = "获取登录日志")
    public ResultInfo<PageInfo<LoginLog>> getLoginLogs(LoginPageParam pageParam) {
        return ResultInfoUtil.buildSuccess(logService.getLoginLog(pageParam));
    }

    @GetMapping("/test")
    public ResultInfo<Void> savelogs() throws IOException {
        logService.saveLoginLogs();
        logService.saveOperationLogs();
        return ResultInfoUtil.buildSuccess();
    }

}